﻿Imports System.Threading

Public Class Form1
    Dim t1 As Thread

    Dim op As System.IO.File
    Dim sFile As String
    Dim sURL As String
    Dim sT As Boolean
    Dim lastSize As Integer, nowSize As Integer, totalSize As Integer
    Dim see As String
    Dim timeout As Integer = 50
    Structure DownInfo
        Dim sURL As String
        Dim sFile As String
    End Structure
    Private Sub sDownFile(ByVal soInfo As DownInfo)
        'On Error Resume Next
        Dim oGetSize As System.Net.WebRequest = System.Net.WebRequest.Create(soInfo.sURL)
        Dim oSize As System.Net.WebResponse = oGetSize.GetResponse
        totalSize = oSize.ContentLength
        'If totalSize < 1024 * 20 Then End
        Try
            Dim s As System.Net.WebClient
            s = New System.Net.WebClient
            s.DownloadFile(soInfo.sURL, soInfo.sFile)
            s.Dispose()
            Debug.Print("about")
        Catch
        End Try
        t1.Abort()
    End Sub


    Public Sub New()
        ' 此调用是设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
    End Sub
    'Private Delegate Sub Meopen(ByVal sURL As String, ByVal sFile As String)
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        'On Error Resume Next
        If strURL.Text = "" Then MsgBox("请填写网页路径", , "提醒") : strURL.Focus() : Exit Sub
        If strSaveTo.Text = "" Then MsgBox("请填写保存路径", , "提醒") : strSaveTo.Focus() : Exit Sub
        Dim sourcei = New DownInfo
        sourcei.sURL = strURL.Text
        sFile = strSaveTo.Text
        sURL = strURL.Text
        sourcei.sFile = strSaveTo.Text
        sT = True
        t1 = New Thread(AddressOf sDownFile)
        timeout = 50
        t1.Start(sourcei)
        't1.Start(strURL.Text, "E:\1.mp3")
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        On Error Resume Next
        If sT = True Then
            If System.IO.File.Exists(sFile) = True Then
                nowSize = FileLen(sFile)
                If lastSize >= 0 And totalSize > 0 Then pb1.Value = CInt((nowSize / 1024) * 100 / (totalSize / 1024))
                Label3.Text = GetSizeString(nowSize) & "/" & GetSizeString(totalSize)      '("E:\2.mp3"  ).
                If nowSize >= lastSize Then
                    Label5.Text = Format((nowSize - lastSize) / 1024, "0.00") & "KB/s"
                    lastSize = nowSize
                End If
            End If
        End If
        If nowSize = totalSize And InStr(see, ",") > 0 And totalSize > 0 Then End
        timeout = timeout - 1
        If timeout = 0 And InStr(see, ",") > 0 Then '当程序时间到，但是任然没有下载完成
            t1.Abort()
            If System.IO.File.Exists(Split(see, ",")(2)) = True Then
                Kill(Split(see, ",")(2))
            End If
            'MsgBox("end")
            End
        End If
    End Sub
    Function GetSizeString(ByVal lpSize As Long)
        If lpSize < 1024 * 1024 Then
            GetSizeString = Format((lpSize / 1024), "0.00") & "KB"
        ElseIf lpSize < 1024 * 1024 * 1024 Then
            GetSizeString = Format((lpSize / 1024 / 1024), "0.00") & "MB"
        Else
            GetSizeString = Format((lpSize / 1024 / 1024 / 1024), "0.00") & "GB"
        End If
    End Function

    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

    End Sub
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        On Error Resume Next
        sT = False
        Dim sii() As String
        sii = System.Environment.GetCommandLineArgs
        For i = 0 To UBound(sii) Step 1
            If see = "" Then
                see = see & sii(i).ToString
            Else
                see = see & "," & sii(i).ToString
            End If
        Next
        'MsgBox(see)
        If InStr(see, ",") > 0 Then
            'MsgBox("hide")

            Dim sourcei = New DownInfo
            sT = True
            sourcei.sURL = Split(see, ",")(1)
            sourcei.sFile = Split(see, ",")(2)
            strURL.Text = sourcei.sURL
            strURL.ReadOnly = True
            strSaveTo.Text = sourcei.sFile
            strSaveTo.ReadOnly = True
            Call Button1_Click(sender, e)
            ' sT = True
            't1 = New Thread(AddressOf sDownFile)
            't1.Start(sourcei)
            Me.Hide()
            'MsgBox("start thread")
        End If
    End Sub
End Class
